#include "timer0.h"
#include "stdio.h"

__WEAK void time0_callback(void){
	
}

void Timer0_Init(void){
	stc_tim0_base_init_t stcTimerCfg;
	stc_irq_regi_conf_t stcIrqRegiConf;
	stc_port_init_t stcPortInit;

	PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM02, Enable);
	stcTimerCfg.Tim0_CounterMode = Tim0_Sync;
	stcTimerCfg.Tim0_SyncClockSource = Tim0_Pclk1;
	stcTimerCfg.Tim0_ClockDivision = Tim0_ClkDiv8;
	stcTimerCfg.Tim0_CmpValue = 6249;//1ms
	TIMER0_BaseInit(M4_TMR02,Tim0_ChannelA,&stcTimerCfg);

	TIMER0_IntCmd(M4_TMR02,Tim0_ChannelA,Enable);
	stcIrqRegiConf.enIRQn = Int002_IRQn;
	stcIrqRegiConf.enIntSrc = INT_TMR02_GCMA;
	stcIrqRegiConf.pfnCallback =&time0_callback;
	enIrqRegistration(&stcIrqRegiConf);
	NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
	NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);
	NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
	TIMER0_Cmd(M4_TMR02,Tim0_ChannelA,Enable);
}

void Timer0_Low_Power_Init(void){
	stc_tim0_base_init_t stcTimerCfg;
	stc_irq_regi_conf_t stcIrqRegiConf;
	stc_port_init_t stcPortInit;

	PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM02, Enable);
	stcTimerCfg.Tim0_CounterMode = Tim0_Sync;
	stcTimerCfg.Tim0_SyncClockSource = Tim0_Pclk1;
	stcTimerCfg.Tim0_ClockDivision = Tim0_ClkDiv8;
	stcTimerCfg.Tim0_CmpValue = 1280;//1ms
	TIMER0_BaseInit(M4_TMR02,Tim0_ChannelA,&stcTimerCfg);

	TIMER0_IntCmd(M4_TMR02,Tim0_ChannelA,Enable);
	stcIrqRegiConf.enIRQn = Int002_IRQn;
	stcIrqRegiConf.enIntSrc = INT_TMR02_GCMA;
	stcIrqRegiConf.pfnCallback =&time0_callback;
	enIrqRegistration(&stcIrqRegiConf);
	NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
	NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);
	NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
	TIMER0_Cmd(M4_TMR02,Tim0_ChannelA,Enable);
}